#include "ConfigInfo.h"
#include <iostream>
#include <sstream>
namespace xncc {
namespace config {

    std::string PlatformLog::toStr() const
    {
        std::stringstream oss;
        oss << "{\n"
            << "LogPath=" << LogPath << ",\n"
            << "LogName=" << LogName << ",\n"
            << "LogLevel=" << LogLevel << ",\n"
            << "CoreId=" << CoreId << ",\n"
            << "RotateSize=" << RotateSize << ",\n"
            << "}\n";
        return oss.str();
    }

    std::string RingQueueInfo::toStr() const
    {
        std::stringstream oss;
        oss << "{\n"
            << "Id=" << Id << ",\n"
            << "Size=" << Size << ",\n"
            << "AutoExpand=" << AutoExpand << ",\n"
            << "}\n";
        return oss.str();
    }

    std::string PubInfo::toStr() const
    {
        std::stringstream oss;
        oss << "{\n"
            << "QueueId=" << QueueId << ",\n"
            << "MsgCodes=[";
        for (auto code : MsgCodes) {
            oss << code << ",";
        }
        oss << "],\n"
            << "}\n";
        return oss.str();
    }

    std::string BusiThreadInfo::toStr() const
    {
        std::stringstream oss;
        oss << "{\n"
            << "Id=" << Id << ",\n"
            << "Name=" << Name << ",\n"
            << "SubIds=[";
        for (auto sid : SubIds) {
            oss << sid << ",";
        }
        oss << "],\n"
            << "PubIds=[";
        for (const auto& pid : PubIds) {
            oss << pid.toStr() << ",";
        }
        oss << "],\n"
            << "CoreId=" << CoreId << "\n"
            << "}\n";
        return oss.str();
    }

    std::string SubServerThreadInfo::toStr() const
    {
        std::stringstream oss;
        oss << "{\n"
            << "Id=" << Id << ",\n"
            << "Name=" << Name << ",\n"
            << "PubId=" << PubId << ",\n"
            << "CoreId=" << CoreId << "\n"
            << "}\n";
        return oss.str();
    }

    std::string ServerThreadInfo::toStr() const
    {
        std::stringstream oss;
        oss << "{\n"
            << "Id=" << Id << ",\n"
            << "Name=" << Name << ",\n"
            << "PubId=" << PubId << ",\n"
            << "Addr=" << Addr << ",\n"
            << "CoreId=" << CoreId << "\n"
            << "SubServerThreads=[\n";
        for (const auto& sub : SubServer) {
            oss << sub.toStr() << ",\n";
        }
        oss << "],\n"
            << "}\n";
        return oss.str();
    }

    std::string ClientThreadInfo::toStr() const
    {
        std::stringstream oss;
        oss << "{\n"
            << "Id=" << Id << ",\n"
            << "Name=" << Name << ",\n"
            << "PubId=" << PubId << ",\n"
            << "Addr=" << Addr << ",\n"
            << "ReConn=" << ReConn << ",\n"
            << "ReConnIntervalMs=" << ReConnIntervalMs << ",\n"
            << "CoreId=" << CoreId << "\n"
            << "}\n";
        return oss.str();
    }

    std::string PlatformInfo::toStr() const
    {
        std::stringstream oss;
        oss << "{\n"
            << "RingQueues=[\n";
        for (auto queue : RingQueues) {
            oss << queue.toStr() << ",\n";
        }
        oss << "],\n"
            << "BusiThreads=[\n";
        for (const auto& btd : BusiThreads) {
            oss << btd.toStr() << ",\n";
        }
        oss << "],\n"
            << "IOThreads=[\n";
        for (const auto& std : ServerThreads) {
            oss << std.toStr() << ",\n";
        }
        for (const auto& ctd : ClientThreads) {
            oss << ctd.toStr() << ",\n";
        }
        oss << "],\n"
            << "}\n";
        return oss.str();
    }

    void from_json(const nlohmann::json& json, PlatformLog& obj)
    {
        json.at("LogPath").get_to(obj.LogPath);
        json.at("LogName").get_to(obj.LogName);
        json.at("LogLevel").get_to(obj.LogLevel);
        if (json.find("CoreId") != json.end()) {
            json.at("CoreId").get_to(obj.CoreId);
        }
        else {
            obj.CoreId = -1;
        }

        if (json.find("RotateSize") != json.end()) {
            json.at("RotateSize").get_to(obj.RotateSize);
        }
        else {
            obj.RotateSize = DEFAULT_ROTATE_MB;
        }
    }

    void from_json(const nlohmann::json& json, RingQueueInfo& obj)
    {
        json.at("Id").get_to(obj.Id);
        json.at("Size").get_to(obj.Size);
        json.at("AutoExpand").get_to(obj.AutoExpand);
    }

    void from_json(const nlohmann::json& json, PubInfo& obj)
    {
        json.at("QueueId").get_to(obj.QueueId);
        json.at("MsgCodes").get_to(obj.MsgCodes);
    }

    void from_json(const nlohmann::json& json, BusiThreadInfo& obj)
    {
        json.at("Id").get_to(obj.Id);
        json.at("Name").get_to(obj.Name);
        json.at("Sub").get_to(obj.SubIds);
        json.at("Pub").get_to(obj.PubIds);
        if (json.find("CoreId") != json.end()) {
            json.at("CoreId").get_to(obj.CoreId);
        }
        else {
            obj.CoreId = -1;
        }
        if (json.find("IOMultiMode") != json.end()) {
            json.at("IOMultiMode").get_to(obj.IOMultiMode);
        }
        else {
            obj.IOMultiMode = 1;
        }
    }

    void from_json(const nlohmann::json& json, SubServerThreadInfo& obj)
    {
        json.at("Id").get_to(obj.Id);
        json.at("Name").get_to(obj.Name);
        json.at("PubId").get_to(obj.PubId);
        if (json.find("CoreId") != json.end()) {
            json.at("CoreId").get_to(obj.CoreId);
        }
        else {
            obj.CoreId = -1;
        }
        if (json.find("IOMultiMode") != json.end()) {
            json.at("IOMultiMode").get_to(obj.IOMultiMode);
        }
        else {
            obj.IOMultiMode = 1;
        }
    }

    void from_json(const nlohmann::json& json, ServerThreadInfo& obj)
    {
        json.at("Id").get_to(obj.Id);
        json.at("Name").get_to(obj.Name);
        json.at("PubId").get_to(obj.PubId);
        json.at("Addr").get_to(obj.Addr);
        if (json.find("CoreId") != json.end()) {
            json.at("CoreId").get_to(obj.CoreId);
        }
        else {
            obj.CoreId = -1;
        }
        if (json.find("IOMultiMode") != json.end()) {
            json.at("IOMultiMode").get_to(obj.IOMultiMode);
        }
        else {
            obj.IOMultiMode = 1;
        }
        if (json.find("BlackList") != json.end()) {
            json.at("BlackList").get_to(obj.BlackList);
        }

        if (json.find("SubServerThreads") != json.end()) {
            json.at("SubServerThreads").get_to(obj.SubServer);
        }
    }

    void from_json(const nlohmann::json& json, ClientThreadInfo& obj)
    {
        json.at("Id").get_to(obj.Id);
        json.at("Name").get_to(obj.Name);
        json.at("PubId").get_to(obj.PubId);
        json.at("Addr").get_to(obj.Addr);
        if (json.find("ReConnIntervalMs") != json.end()) {
            json.at("ReConnIntervalMs").get_to(obj.ReConnIntervalMs);
            obj.ReConn = true;
        }
        else {
            obj.ReConnIntervalMs = -1;
            obj.ReConn           = false;
        }
        if (json.find("CoreId") != json.end()) {
            json.at("CoreId").get_to(obj.CoreId);
        }
        else {
            obj.CoreId = -1;
        }
        if (json.find("IOMultiMode") != json.end()) {
            json.at("IOMultiMode").get_to(obj.IOMultiMode);
        }
        else {
            obj.IOMultiMode = 1;
        }
    }

    void from_json(const nlohmann::json& json, PlatformInfo& obj)
    {
        if (json.find("PlatformLog") != json.end()) {
            json.at("PlatformLog").get_to(obj.LogInfo);
        }
        else {
            std::ostringstream oss;
            oss << "platform log use default config {LogPath=" << obj.LogInfo.LogPath
                << ", LogName=" << obj.LogInfo.LogName << ", LogLevel=" << obj.LogInfo.LogLevel
                << ", CoreId=" << obj.LogInfo.CoreId << ", RotateSize=" << obj.LogInfo.RotateSize << "M}";
            std::cout << oss.str() << '\n';
        }
        json.at("Queues").get_to(obj.RingQueues);
        json.at("BusinessThreads").get_to(obj.BusiThreads);

        if (json.find("ServerThreads") != json.end()) {
            json.at("ServerThreads").get_to(obj.ServerThreads);
        }

        if (json.find("ClientThreads") != json.end()) {
            json.at("ClientThreads").get_to(obj.ClientThreads);
        }
    }
}  // namespace config
}  // namespace xncc